package com.xbongbong.crm.handle.analysis;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.BusinessUserTeamHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.parse.data.AbstractListAnalysisDataService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;

/**
 * @author 吴峰
 * @date 2022/05/23 15:17
 */
@Service("clueListAnalysisDataHandler")
public class ClueListAnalysisDataHandler extends AbstractListAnalysisDataService<PaasFormDataEntityExt> {

    @Resource
    private BusinessUserTeamHelp businessUserTeamHelp;

    @Override
    public List<Integer> businessList() {
        return Arrays.asList( XbbRefTypeEnum.CLUE.getCode());
    }


    @Override
    protected Map<Long, List<String>> getUserTeamMap(Set<String> userIdList, List<Long> dataIdList, String corpid, Integer businessType, Map<Long, List<String>> mainUserIdMap,
                                                     Map<Long, List<String>> coUserIdMap, Integer subBusinessType) throws XbbException {
        Boolean isPublic = Objects.equals(XbbRefTypeEnum.CLUE_PUBLIC.getCode(),subBusinessType);
        businessUserTeamHelp.getUserIdMapSetShow(userIdList,dataIdList,corpid,businessType,isPublic,mainUserIdMap, coUserIdMap, DelEnum.NORMAL.getDel());
        return new HashMap<>(BasicConstant.ZERO);
    }

    @Override
    protected boolean publicIsVisible(JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (!Objects.equals(FieldTypeEnum.CONTACT_NUMBER.getType(), fieldAttrEntity.getFieldType())) {
            return false;
        }
        Integer isPublic = getIntegerOrDefaultFromFormData(data, ClueEnum.IS_PUBLIC.getAttr(), BasicConstant.ZERO);

        Integer publicPool = fieldAttrEntity.getPublicPool();
        Integer showPublicPool = fieldAttrEntity.getShowPublicPool();
        return Objects.equals(isPublic, BasicConstant.ONE) && Objects.equals(showPublicPool, BasicConstant.ONE) && Objects.equals(publicPool, BasicConstant.ZERO);
    }
}
